<template>
	<view class="notice">
		<!-- 1，顶部导航 -->
		<notice-navbar :activeIndex="activeIndex" @changeActive="changeActive"></notice-navbar>

		<!-- 2，列表展示 -->
		<!-- 2.1，我发送的 -->
		<notice-list-my :noticeListData="noticeListData" v-if="activeIndex == 1" @refData="refData"
			@cleanSystemMsg="cleanSystemMsg"></notice-list-my>
		<!-- 2.2，我接收的 -->
		<notice-list :noticeListData="noticeListData" v-if="activeIndex == 2" @refData="refData"
			@cleanSystemMsg="cleanSystemMsg"></notice-list>
	</view>
</template>

<script>
	//1，导入子组件
	import noticeNavbar from './notice-child/notice-navbar.vue'
	import noticeList from './notice-child/notice-list.vue'
	import noticeListMy from './notice-child/notice-list-my.vue'

	//2，导入网络请求
	import {
		getMsgBySystem,
		cleanAll,
		deleteById
	} from '@/api/http/msg/system_msg.js'

	//3，引入vuex
	import {
		mapActions
	} from 'vuex'

	export default {
		components: {
			noticeNavbar,
			noticeList,
			noticeListMy
		},
		data() {
			return {
				activeIndex: 1, //系统类型
				noticeListData: [], //系统消息

				page: 1,
				size: 30,
			}
		},
		methods: {
			...mapActions('m_chat', ['readSysNum']),
			//1，修改选中
			changeActive(id, item) {
				this.activeIndex = id
				this.noticeListData = []

				//发起网络请求
				this.queryNoticeMsg(id)
			},
			//2，查询系统消息
			queryNoticeMsg(activeType) {
				getMsgBySystem(this.page, this.size, activeType).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//console.log(res.data.busInfo)
					this.noticeListData = res.data.busInfo
				})
			},
			//3，更新消息
			refData() {
				this.page = 1
				this.size = 30
				this.queryNoticeMsg(this.activeIndex)
			},
			//4，删除一条系统消息
			cleanSystemMsg(id) {
				deleteById(id).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//return uni.$showMsg('删除成功');
					setTimeout(() => {
						this.refData()
					}, 100)

				})
			}

		},
		onLoad() {
			this.queryNoticeMsg(1)
		},
		onShow() {
			//消息已读
			this.readSysNum()
		},
		async onNavigationBarButtonTap(e) {
			var that = this
			uni.$isShowModal('是否清空系统消息？').then(resultState => {
				if (resultState) {
					cleanAll().then(res => {
						const result = res.data
						if (result.pubInfo.code !== 0) {
							return uni.$showMsg(result.pubInfo.message);
						}
						that.refData()
					})
				}
			})
		}
	}
</script>

<style scoped lang="scss">

</style>